package com.yanqu.road.server.gameplayer.module.messagesubscribe;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.messageSubscribe.entity.ChannelTemplateInfo;
import com.yanqu.road.pb.messagesubscribe.MessageSubscribe;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.messagesubscribe.DyMessageSubscribe;
import com.yanqu.road.server.manger.messagesubscribe.MessageSubscribeMgr;
import com.yanqu.road.entity.messageSubscribe.entity.MessageSubscribeInfo;
import com.yanqu.road.entity.messageSubscribe.entity.MessageSubscribePlayerData;
import com.yanqu.road.server.manger.messagesubscribe.ZfbMessageSubscribe;
import com.yanqu.road.server.manger.messagesubscribe.enums.MessageSubscribeConfig;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 微信消息订阅
 * @author ：cwq
 * @date ：2023/5/6 12:00
 */
public class MessageSubscribeModule extends GeneralModule {

    public MessageSubscribeModule(GamePlayer player) {
        super(player);
    }


    public int setUserOp(String templateId,boolean isDingyue,boolean isHuanqi){
        MessageSubscribePlayerData playerData = MessageSubscribeMgr.getUserData(player.getUserId());
        if(playerData != null){
            Map<String, MessageSubscribeInfo> sbMap = playerData.getSubscribedMap();
            if(sbMap.containsKey(templateId)){
                boolean update = false;
                MessageSubscribeInfo info = sbMap.get(templateId);
                if(isDingyue){
                    info.setDingyueTimes(info.getDingyueTimes() + 1);
                    update = true;
                }
                if(isHuanqi){
                    info.setHuanqiTimes(info.getHuanqiTimes() + 1);
                    update = true;
                }
                if(update){
                    playerData.setSubscribedMap(sbMap);
                }
            }
        }
        return 0;
    }

    public int setSubscribe(MessageSubscribe.SetMessageSubscribeDataReqMsg msg) {
        MessageSubscribePlayerData playerData = MessageSubscribeMgr.getUserData(player.getUserId());
        if(playerData != null){
            playerData.setIsSwitch(msg.getSwitch());

            playerData.setOpenId(msg.getWxOpenId());
            for(MessageSubscribe.SetMessageSubscribeData setMsg : msg.getSetListList()){
                MessageSubscribeMgr.setSubscribe(player.getUserId(),setMsg.getTemplateId(),setMsg.getIsSubscribeOfGame(),setMsg.getIsSubscribeOfWx());
            }
        }else{
            return 0;
        }
        MessageSubscribe.SetMessageSubscribeDataRspMsg.Builder respMsg = MessageSubscribe.SetMessageSubscribeDataRspMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setPlayerData(parsePlayerDataMsg(playerData));
        player.sendPacket(Protocol.U_MESSAGE_SUBSCRIBE_SET, respMsg);
        return 0;
    }

    private MessageSubscribe.MessageSubscribePlayerDataMsg.Builder parsePlayerDataMsg(MessageSubscribePlayerData playerData){
        MessageSubscribe.MessageSubscribePlayerDataMsg.Builder playerMsg = MessageSubscribe.MessageSubscribePlayerDataMsg.newBuilder();
        playerMsg.setSwitch(playerData.isSwitch());
        Map<String, MessageSubscribeInfo> sbMap = new ConcurrentHashMap<>(playerData.getSubscribedMap());
        for(MessageSubscribeInfo sbInfo : sbMap.values()){
            MessageSubscribe.MessageSubscribeData.Builder sbInfoMsg = MessageSubscribe.MessageSubscribeData.newBuilder();
            sbInfoMsg.setTemplateId(sbInfo.getTemplateId());
            sbInfoMsg.setIsSubscribeOfGame(sbInfo.isSubscribeOfGame());
            sbInfoMsg.setIsSubscribeOfWx(sbInfo.isSubscribeOfWx());
            playerMsg.addMessageSubscribeDataList(sbInfoMsg);
        }
        return playerMsg;
    }

    public void syncUserData(){
        Map<String, ChannelTemplateInfo> channelTemplateInfoMap = MessageSubscribeMgr.getChannelTemplateInfoMap();
        MessageSubscribePlayerData playerData = MessageSubscribeMgr.getUserData(player.getUserId());
        if(playerData != null){
            MessageSubscribe.MessageSubscribeInfoMsg.Builder syncMsg = MessageSubscribe.MessageSubscribeInfoMsg.newBuilder();
            syncMsg.setPlayerData(parsePlayerDataMsg(playerData));
            for(MessageSubscribeConfig type : MessageSubscribeConfig.values()){
                MessageSubscribe.MessageSubscribeConfig.Builder configMsg = MessageSubscribe.MessageSubscribeConfig.newBuilder();
                if (DyMessageSubscribe.isSwitch()) {
                    if (type.getDyTemplateId().isEmpty()) {
                        continue;
                    }
                    configMsg.setTemplateId(type.getDyTemplateId());
                }
                if (ZfbMessageSubscribe.isSwitch()) {
                    if (type.getZfbTemplateId().isEmpty()) {
                        continue;
                    }
                    configMsg.setTemplateId(type.getZfbTemplateId());
                }
                if (MessageSubscribeMgr.isWxSwitch()) {
                    if (type.getWxTemplateId().isEmpty()) {
                        continue;
                    }
                    configMsg.setTemplateId(type.getWxTemplateId());
                }
                configMsg.setTitle(type.getGameTitle());
                configMsg.setContent(type.getGameContent());
                configMsg.setPushRate(1);
                configMsg.setPushRateUnit(7);
                // 配置使用同一份，不过要配置对应渠道id的配置，模板id都使用微信的即可
                if(channelTemplateInfoMap.containsKey(type.getWxTemplateId())){
                    configMsg.setPushRateUnit(channelTemplateInfoMap.get(type.getWxTemplateId()).getPushRateUnit());
                }
                syncMsg.addMessageSubscribeConfigList(configMsg);
            }
            player.sendPacket(Protocol.U_MESSAGE_SUBSCRIBE_SYN, syncMsg);
        }
    }


    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        return false;
    }

    @Override
    public void afterLogin() {
        syncUserData();
    }
}
